package com.huluobo.a2302mdmdemo.videos.pages

import android.util.Log
import android.widget.Toast
import android.widget.VideoView
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2302mdmdemo.videos.VideoIntent
import com.huluobo.a2302mdmdemo.videos.VideoViewModel
import com.huluobo.lib_base.UIState
import com.huluobo.lib_base.entity.VideoEntity
import com.huluobo.lib_base.entity.VideoEntityItem
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/8/5.
 */

@Composable
fun CollectVideoPage(navHostController: NavHostController, vm: VideoViewModel = hiltViewModel()) {
    val list = remember {
        SnapshotStateList<VideoEntityItem>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnFailed -> {
                        ToastUtils.showLong("获取收藏列表失败:${it.msg}")
                        Log.e("TagA", "获取收藏列表失败${it.msg}")
                    }

                    UIState.OnLoading -> {}
                    is UIState.OnSuccess<*> -> {
                        list.clear()
                        list.addAll(it.data as List<VideoEntityItem>)
                        Log.e("TagA", "collect:${list.size}")
                    }
                }
            }
        }
        vm.sendIntent(VideoIntent.GetCollectVideos)
    })
}